package com.shanghaionstar.weight;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.weight.CustomImageView;
import com.shanghaionstar.R;

/**
 * Created by 7828wc on 16/7/13.
 */
public class CommonNoDatasLayout extends RelativeLayout {
    private CustomImageView refresh_image;
    private TextView tv_no_datas;
    private View view;

    public CommonNoDatasLayout(Context context) {
        super(context);
        initView(null);
    }

    public CommonNoDatasLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(attrs);
    }

    public CommonNoDatasLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(attrs);
    }

    private void initView(AttributeSet attrs) {
        view = inflate(getContext(), R.layout.common_no_datas_layout, this);
        tv_no_datas = (TextView) view.findViewById(R.id.tv_no_datas);
        refresh_image = (CustomImageView) view.findViewById(R.id.refresh_image);

        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, com.onstar.cn.weight.R.styleable.no_datas_layout);
            int showIcon = typedArray.getResourceId(com.onstar.cn.weight.R.styleable.no_datas_layout_showIcon, -1);
            String hintText = typedArray.getString(com.onstar.cn.weight.R.styleable.no_datas_layout_showHintText);

            if (showIcon != -1) {
                refresh_image.setImageResource(showIcon);
                refresh_image.setVisibility(View.VISIBLE);
            } else {
                refresh_image.setVisibility(View.GONE);
            }

            tv_no_datas.setText(hintText);
            tv_no_datas.setVisibility(CommonUtil.isNull(hintText) ? View.GONE : View.VISIBLE);
            typedArray.recycle();
        }
    }

    public void setHintText(String hintText){
        tv_no_datas.setText(hintText);
        tv_no_datas.setVisibility(CommonUtil.isNull(hintText) ? View.GONE : View.VISIBLE);
    }

    public void showIcon(boolean ifShow){
        refresh_image.setVisibility(ifShow ? View.VISIBLE : View.GONE);
    }

    public void setRefreshImage(int imgId){
        refresh_image.setImageResource(imgId);
    }

    public void setHintText(int resId){
        setHintText(getContext().getString(resId));
    }

    public void setOnRefreshListener(OnClickListener onClickListener){
        refresh_image.setOnClickListener(onClickListener);
    }
}
